package com.whale.cloud.util.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import lombok.Data;

/** Created by Abel on 2017/6/13. */
@Data
@Component
public class ZSetOps {

  @Autowired private RedisTemplate<Object, Object> redisTemplate;

  /**
   * 添加集合元素
   *
   * @param key
   * @param item
   * @return
   */
  public boolean addItem(String key, Object item, double score) {
    ZSetOperations<Object, Object> zSetOperations = redisTemplate.opsForZSet();
    zSetOperations.add(key, item, score);
    return true;
  }

  /**
   * 移除集合元素
   *
   * @param key
   * @param item
   * @return
   */
  public boolean removeItem(String key, Object... item) {
    ZSetOperations<Object, Object> zSetOperations = redisTemplate.opsForZSet();
    zSetOperations.remove(key, item);
    return true;
  }

  /**
   * 获取集合长度
   *
   * @param key
   * @return
   */
  public Long getSize(String key) {
    ZSetOperations<Object, Object> zSetOperations = redisTemplate.opsForZSet();
    return zSetOperations.size(key);
  }
}
